#include "keyword.h"
#include <algorithm>
#include <math.h>

KeyWord::KeyWord(void)
{
_traningMgr	= TraningManager::getInstance();
}

KeyWord::~KeyWord(void)
{
}

bool KeyWord::keyIsExist(const string& keyName)
{
	return ( _wordMap.find(keyName) != _wordMap.end() );
}

struct compByValLittle{
	bool operator()(const pair<string, double>& lhs, const pair<string, double>& rhs)
	{
		return lhs.second < rhs.second;
	}
};

struct compByValBig{
	bool operator()(const pair<string, double>& lhs, const pair<string, double>& rhs)
	{
		return lhs.second < rhs.second;
	}
};


vector<string> KeyWord::getKeyWord(vector<string> &strs)
{
	vector<string>::iterator wordIter;
	for (wordIter = strs.begin(); wordIter != strs.end(); ++wordIter)
	{
		string str = *wordIter;
		if (!keyIsExist(str))
		{
			_wordMap[str] = 1;
		}else{
			_wordMap[str]++;
		}
	}

	vector<pair<string,double>> sortWordVec(_wordMap.begin(), _wordMap.end());
	sort(sortWordVec.begin(), sortWordVec.end(),compByValBig());

	double termsCount = strs.size();
	double trainingCount = _traningMgr->getTrainingCount();

	vector<pair<string, double>>::iterator iter;
	int cnt;
	for (iter = sortWordVec.begin(), cnt=0; iter!=sortWordVec.end()&& cnt<=20; ++iter, ++cnt)
	{
		double tf = iter->second / termsCount;
		double keyCount = _traningMgr->getFileCountOfIDF(iter->first); 
		double count = trainingCount / (keyCount + 1);
		double idf = log10(count);
		double tfidf = tf * idf;
		iter->second = tfidf;
	}
	sort(sortWordVec.begin(), sortWordVec.end(),compByValLittle());

	vector<string> keyWords;
	for(int i = 0; i < 10; i++)
	{
		keyWords.push_back(sortWordVec.at(i).first);
	}

	return keyWords;
}
